﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DatabaseAdminTools.Connections;

namespace DatabaseAdminTools.MiniForms
{
    public partial class DatabaseForm : Form
    {
        private Connection conn;
        private bool create;
        private string[] info;
        private string dbName;

        public DatabaseForm(Connection conn)
        {
            InitializeComponent();
            this.conn = conn;
            create = true;
        }

        public DatabaseForm(Connection conn, string _dbName)
        {
            InitializeComponent();
            this.conn = conn;
            create = false;
            dbName = _dbName;
            textBox1.Text = dbName;
            textBox1.ReadOnly = true;
            button1.Enabled = true;
            this.Text = "Database properties";
        }

        private void CreateDatabaseForm_Load(object sender, EventArgs e)
        {
            charsetCBox.Items.Add("DEFAULT");
            collationCBox.Items.Add("DEFAULT");
            System.Threading.Thread t = new System.Threading.Thread(new WaitDialog("Getting Character set and collation from database...").show);
            t.Start();
            try
            {
                foreach (string str in conn.getCharacterSet())
                {
                    charsetCBox.Items.Add(str);
                }
                foreach (string str in conn.getCollation())
                {
                    collationCBox.Items.Add(str);
                }
                if (create)
                {
                    charsetCBox.SelectedItem = "DEFAULT";
                    collationCBox.SelectedItem = "DEFAULT";
                }
                else
                {
                    info = conn.getDatabaseInfo(dbName);
                    if (charsetCBox.Items.Contains(info[0]))
                    {
                        charsetCBox.SelectedItem = info[0];
                    }
                    if (collationCBox.Items.Contains(info[1]))
                    {
                        charsetCBox.SelectedItem = info[1];
                    }
                }
            }
            catch (Exception ex)
            {
                t.Abort();
                MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            t.Abort();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            WaitDialog dialog = new WaitDialog();
            System.Threading.Thread t = new System.Threading.Thread(dialog.show);
            string charset = charsetCBox.SelectedItem.ToString();
            string collation = collationCBox.SelectedItem.ToString();
            try
            {
                if (create)
                {
                    dialog.Text = "Creating database...";
                    t.Start();
                    ((Form1)this.Owner).createNewDatabase(conn, textBox1.Text, charset, collation);
                }
                else
                {
                    if (!charset.Equals(info[0]) || !collation.Equals(info[1]))
                    {
                        dialog.Text = "Updating database information...";
                        t.Start();
                        conn.updateDatabaseInfo(textBox1.Text, charset, collation);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            t.Abort();
            this.Dispose();
        }

        private void charsetCBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (Object obj in collationCBox.Items)
            {
                if (obj.ToString().StartsWith(charsetCBox.SelectedItem.ToString()))
                {
                    collationCBox.SelectedItem = obj;
                    break;
                }
            }
        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            button1.Enabled = !textBox1.Text.Equals("");
        }
    }
}
